package com.bruce.demo.sequence.dao;

import com.bruce.demo.sequence.dto.SequenceInfoDto;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luotuan
 * @Description 序列表dao
 * @create 2020-04-09 17:05
 **/
@Repository
public class SequenceDaoImpl implements SequenceDao {
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public List<SequenceInfoDto> queryAll() {
        String sql = "select " + cloums + " from sequence_info ";
        List<SequenceInfoDto> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SequenceInfoDto.class));
        if (CollectionUtils.isNotEmpty(result)) {
            return result;
        }
        return Collections.emptyList();
    }

    @Override
    public SequenceInfoDto queryOne(String sysCode, String code)  {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select " + cloums + " from sequence_info ");
        sqlBuilder.append("where sys_code =:sysCode and code=:code ");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("sysCode", sysCode);
        paramMap.put("code", code);
        List<SequenceInfoDto> result = jdbcTemplate.query(sqlBuilder.toString(), paramMap, new BeanPropertyRowMapper<>(SequenceInfoDto.class));
        if (CollectionUtils.isNotEmpty(result)) {
            return result.get(0);
        }
        return null;
    }

    @Override
    public boolean addSequence(SequenceInfoDto sequenceInfo)  {
        StringBuilder sqlBuilder = new StringBuilder("insert into sequence_info (" + cloums2 + ") values ");
        sqlBuilder.append("(:sysCode,:code,:serverCacheSize,:clientIncrementSize,:clientCacheSize,:minValue,:state,:remarks,:createTime)");
        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(sequenceInfo);
        return jdbcTemplate.update(sqlBuilder.toString(), parameterSource) > 0;
    }

    @Override
    public Long getMinValue(String sysCode, String code) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select min_value as minValue from sequence_info ");
        sqlBuilder.append("where sys_code=:sysCode and code=:code");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("sysCode", sysCode);
        paramMap.put("code", code);
        return jdbcTemplate.queryForObject(sqlBuilder.toString(), paramMap, Long.class);
    }

    @Override
    public boolean updateMinValue(String sysCode, String code, long minValue)  {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("update sequence_info set min_value=:minValue ");
        sqlBuilder.append("where sys_code=:sysCode and code=:code");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("minValue", minValue);
        paramMap.put("sysCode", sysCode);
        paramMap.put("code", code);
        return jdbcTemplate.update(sqlBuilder.toString(), paramMap) > 0;
    }
}
